package com.red.wood.common.springjdbc;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;

@Repository
public class SqlServerDao {

	private static final Log log = LogFactory.getLog(SqlServerDao.class);
	
	@Autowired
	@Qualifier("jdbcTemplateSqlServer")
	private JdbcTemplate jdbcTemplate;
	
	public IMap getUserLevelInfo(String userId) {
		IMap param = new IMap();
		try {
			List<Map<String,Object>> list = jdbcTemplate.queryForList("select pkid,username,mobi,exfield1,exfield2,exfield3 from Member where pkid="+userId);
			int userLevel = 1;
			String resultInfo = "";
			if(list != null && list.size() > 0) {
				Map<String,Object> map = list.get(0);
				String username = CommonUtils.valueOf(map.get("username"));
				int mobi = Integer.parseInt(CommonUtils.valueOf(map.get("mobi")));
				if(mobi == 4 || mobi == 9) {//工信部 超级管理员
					userLevel = 0;//超级管理权限
				}else {
					String exfield1 = CommonUtils.valueOf(map.get("exfield1"));//企业
					String exfield2 = CommonUtils.valueOf(map.get("exfield2"));//机房
					String exfield3 = CommonUtils.valueOf(map.get("exfield3"));//客户
					if(StringUtils.equals(exfield1, "True") && StringUtils.equals(exfield2, "False") && StringUtils.equals(exfield3, "False")) {//勾选企业
						userLevel = 1;
						resultInfo = "当前登录用户【"+username+"】查询权限：勾选【企业】";
					}else if(StringUtils.equals(exfield1, "False") && StringUtils.equals(exfield2, "True") && StringUtils.equals(exfield3, "False")) {//勾选机房
						userLevel = 2;
						resultInfo = "当前登录用户【"+username+"】查询权限：勾选【机房】";
					}else if(StringUtils.equals(exfield1, "False") && StringUtils.equals(exfield2, "False") && StringUtils.equals(exfield3, "True")) {//勾选客户
						userLevel = 3;
						resultInfo = "当前登录用户【"+username+"】查询权限：勾选【客户】";
					}else if(StringUtils.equals(exfield1, "True") && StringUtils.equals(exfield2, "True") && StringUtils.equals(exfield3, "False")) {//勾选企业、机房
						userLevel = 4;
						resultInfo = "当前登录用户【"+username+"】查询权限：勾选【企业、机房】";
					}else if(StringUtils.equals(exfield1, "True") && StringUtils.equals(exfield2, "False") && StringUtils.equals(exfield3, "True")) {//勾选企业、客户
						userLevel = 5;
						resultInfo = "当前登录用户【"+username+"】查询权限：勾选【企业、客户】";
					}else if(StringUtils.equals(exfield1, "False") && StringUtils.equals(exfield2, "True") && StringUtils.equals(exfield3, "True")) {//勾选机房、客户
						userLevel = 6;
						resultInfo = "当前登录用户【"+username+"】查询权限：勾选【机房、客户】";
					}else if(StringUtils.equals(exfield1, "True") && StringUtils.equals(exfield2, "True") && StringUtils.equals(exfield3, "True")) {//勾选企业、机房、客户
						userLevel = 7;
						resultInfo = "当前登录用户【"+username+"】查询权限：勾选【企业、机房、客户】";
					}else {
						userLevel = 7;//都没有勾选，默认查询最低权限
						resultInfo = "当前登录用户【"+username+"】查询权限：都没有勾选，默认查询【企业、机房、客户】";
					}
				}
				param.put("userLevel", userLevel);
				param.put("loginUser", username);
				//param.put("mobi", CommonUtils.valueOf(map.get("mobi")));
				log.info(resultInfo);
				log.info("userLevel："+userLevel);
			}
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return param;
	}
	
	public IMap getEnterpriseAuthority(String userId) {
		IMap param = new IMap();
		try {
			List<Map<String,Object>> list = jdbcTemplate.queryForList("select agentArea,mobi,realName,username from Member where pkid="+userId);
			int userAuthority = 0;
			if(list != null && list.size() > 0) {
				Map<String,Object> map = list.get(0);
				String username = CommonUtils.valueOf(map.get("username"));
				String realName = CommonUtils.valueOf(map.get("realName"));
				int agentArea = Integer.parseInt(CommonUtils.valueOf(map.get("agentArea")));
				int mobi = Integer.parseInt(CommonUtils.valueOf(map.get("mobi")));
				
				if(agentArea == 2) {
					if(mobi == 1) {//管局账号
						userAuthority = 1;
					}else if(mobi == 2) {//集团账号(移动、电信、联通)
						userAuthority = 2;
					}else if(mobi == 3) {//省级基础电信企业(分省和运营商),如北京电信
						userAuthority = 3;
					}else if(mobi == 6) {//增值企业
						userAuthority = 4;
					}else if(mobi == 9) {//超级管理员
						userAuthority = 5;
					}
				}
				
				param.put("userAuthority", userAuthority);
				param.put("loginUser", username);
				param.put("realName", realName);
				log.info(param);
			}
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return param;
	}
}
